import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

import {ResultData} from "../bean/ResultData";
import { Api } from 'src/app/api/Api';
import {environment} from "../../../environments/environment";

@Component({
    selector: 'app-ajax-send',
    templateUrl: './ajax-send.component.html',
    styleUrls: ['./ajax-send.component.less']
})
export class AjaxSendComponent implements OnInit {
    types=[];
    films=[];
    size=3;
    typeId=10003;
    wd="";
    constructor(
        public hc: HttpClient,
        public api:Api
    ) { }

    ngOnInit() {
    }
    printHttp(){
        console.log(this.hc);
    }
    sendGetNoParams(){
        console.log("无参数请求-get");
        // let obs=this.hc.get("/api/film-project/filmApi/loadTyps.php");
        let obs=this.hc.get(environment.baseurl+"/film-project/filmApi/loadTyps.php");
        obs.subscribe((data:ResultData)=>{
            // console.log(data);
            // console.log(data.result);
            this.types=data.result;
        },(error)=>{
            console.log(error);
        })
    }
    sendGetNoParams1(){
        // this.hc.get("/api/film-project/filmApi/loadTyps.php")
        this.hc.get(environment.baseurl+"/film-project/filmApi/loadTyps.php")
            .toPromise()
            .then((data:ResultData)=>{
                this.types=data.result;
            })
    }
    sendGetHaveParams(){
        // this.hc.get("/api/film-project/filmApi/loadFilms.php?size="+this.size+"&typeId="+this.typeId)
        this.hc.get(environment.baseurl+"/film-project/filmApi/loadFilms.php?size="+this.size+"&typeId="+this.typeId)
            .toPromise()
            .then((data:ResultData)=>{
                this.films=data.result;
            })
    }
    sendGetHaveParams1(){
        // this.hc.get("/api/film-project/filmApi/loadFilms.php",{
        this.hc.get(environment.baseurl+"/film-project/filmApi/loadFilms.php",{
            params:{
                size:this.size+"",
                typeId:this.typeId+""
            }
        }).toPromise()
            .then((data:ResultData)=>{
                this.films=data.result;
            })
    }
    sendPostNoParams(){
        // this.hc.post("/api/film-project/filmApi/loadTyps.php",null)
        this.hc.post(environment.baseurl+"/film-project/filmApi/loadTyps.php",null)
            .toPromise()
            .then((data:ResultData)=>{
                this.types=data.result;
            })
    }
    sendPostHaveParams(){
        // this.hc.post("/api/film-project/filmApi/loadFilms.php",`size=${this.size}&typeId=${this.typeId}`,{
        this.hc.post(environment.baseurl+"/film-project/filmApi/loadFilms.php",`size=${this.size}&typeId=${this.typeId}`,{
            headers:{
                "Content-Type":"application/x-www-form-urlencoded"
            }
        })
            .toPromise()
            .then((data:ResultData)=>{
                this.films=data.result;
            })
    }
    sendPostHaveParams1(){
        let hp=new HttpParams();
        hp=hp.append("size",this.size+"");
        hp=hp.append("typeId",this.typeId+"");
        console.log(hp);
        console.log(hp.toString());
        // this.hc.post("/api/film-project/filmApi/loadFilms.php",hp)
        this.hc.post(environment.baseurl+"/film-project/filmApi/loadFilms.php",hp)
            .toPromise()
            .then((data:ResultData)=>{
                this.films=data.result;
            })
    }
    sendJsonp(){
        this.hc.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+this.wd,"cb")
            .toPromise()
            .then((data)=>{
                console.log(data);
            })
    }
    sendModule(){
        this.api.loadTypesData()
            .then((data:ResultData)=>{
                this.types=data.result;
            })
            .catch((error)=>{
                console.log(error);
            })
    }
}
